﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ERP.DAL.Util;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using ERP.BusinessEntities.Base;
using ERP.BusinessEntities.Factory;
using System.Drawing;       
using System.IO;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ERP.DAL
{
    public class Host
    {
        //public static DAL.SQLDal sqldal;// = new DAL.SQLDal();
        public static DAL.EntDal entdal;
        public static Database db;
        public static SqlConnection SqlConn;
        public static SqlDatabase SqlDb;
        public static SQLDBUtil sqlDbUtil = new SQLDBUtil();
        public Host(Database _db)
        {
            db = _db;
            //SqlConn = _SqlConn;
            //SqlDb = _SqlDb;
        }

        public static Dictionary<String, BaseBusinessController> lstControllers = new Dictionary<String, BaseBusinessController>();
        public static Dictionary<String, DataSet> lstDatasets = new Dictionary<String, DataSet>();

        public static DataSet GetDataset(String strTable)
        {
            if (lstDatasets.ContainsKey(strTable) == false)
            {
                BaseBusinessController Ctrl = GetCtrl(strTable);
                DataSet ds = Ctrl.GetAllObjects();
                //return ds;
                lstDatasets.Add(strTable, ds);
            }
            return lstDatasets[strTable];
        }
        public static BaseBusinessController GetCtrl(String strTable)
        {
            if (lstControllers.ContainsKey(strTable) == false)
                lstControllers.Add(strTable, (BaseBusinessController)BusinessFactory.CreateBusinessObject(strTable + "Controller"));

            return lstControllers[strTable];
        }

        public static Image GetImageByFileName(String strFileName)
        {
            String[] strFiles = Directory.GetFiles(Application.StartupPath + "\\image", "*" + strFileName + "*");
            Image img = null;
            if (strFiles.Length > 0)
            {
                String strPath = strFiles[0];

                img = Image.FromFile(strPath);
            }
            if(img == null)
                img = Image.FromFile(Application.StartupPath + "\\image\\default.ico");
            if(img != null)
                img = ResizeImage(img, new Size(23,20));

            return img;
        }

        public static Image ResizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

    }
}
